<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	活动列表type=3表单页面
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$upwebpage=pdo_getall('core_mobile_web',array('plugin_name'=>'','uniacid'=>$_SESSION['uniacid']),array('id','plugin_name'));
	if($upwebpage){
		foreach($upwebpage as &$v){
			pdo_update('core_mobile_web',array('plugin_name'=>'core'),array('id'=>$v['id'],'uniacid'=>$_SESSION['uniacid']));
		}
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>3,'webid'=>0);
	if($_RQ['group_id']){
		$params['group_id']=intval($_RQ['group_id']);
		$return['params']['group_id']=$_RQ['group_id'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','group_id','createtime','status','html','plugin_name','pageaction');
	$list=pdo_getslice('core_mobile_web',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($list){
		$default_mobile_web=uni_setting('default_mobile_web');
		foreach($list as &$item){
			$item['is_default']=($item['id']==$default_mobile_web)?1:0;
			$item['plugin_title']=$item['plugin_name']=='core'?'系统':pdo_getcolumn('core_plugins',array('name'=>$item['plugin_name']),'title');
			$item['url']=custom_url('core/welcome//id='.$item['id'],true);
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'活动列表'),
			array('action'=>'/basic.xiu_form/edit/','title'=>'添加活动'),
			array('action'=>'/basic.xiu_formtemplate/my/','title'=>'我的模板'),
		),
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：每个活动是一个整体，活动下面可以创建多个页面，比如：首页，文章列表页，产品页，内容页等配套使用就形成一个丰富的整体！','list'=>array(
			array('name'=>'title','title'=>'活动名称','type'=>'string'),
			array('name'=>'plugin_title','title'=>'所属插件','type'=>'string'),
			array('name'=>'is_default','title'=>'默认活动','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'启用/关闭','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'url','title'=>'体验二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('action'=>'basic.xiu_formtpls/','title'=>'DIY布局','type'=>'href'),
			//array('action'=>'website.mobile_page/','title'=>'子页面管理','type'=>'href'),
			//array('op'=>'release','title'=>'保存到我的模版','type'=>'href'),
			//array('op'=>'release_wxapp','title'=>'保存为小程序模版','type'=>'href'),
		),'action'=>array(
			array('type'=>'href','action'=>'/wxapp.list/edit/fromweb=true','title'=>'小程序设置'),
			array('type'=>'href','action'=>'/wxapp.list/up_app/fromweb=true','title'=>'上传小程序'),
		)),
		'search_one'=>array('select_name'=>'group_id','options'=>'groups','name'=>'title','add'=>false,'select_title'=>'所属分组'),
	);
	if($_SESSION['isfounder']){
		$htmls['tabs'][]=array('action'=>'cloud.mobile_blocks','op'=>'prepared','title'=>'安装页面组件(管理员可见)');
	}
	$return['htmls']=$htmls;
	$return['groups']=pdo_getall('core_mobile_web_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	set_html('header','common/header');
	set_html('title','微官网');
	exi($return);
}
if($_RQ['op']=='edit'){
	//编辑活动，添加活动在活动模版列表那边选择模版然后添加
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_mobile_web',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title','status','nopc','domain','foot','wxapp_id','menu_id','syscolor'));	
	}else{
		$item=array();
	}
	if($item['wxapp_id']){
		$item['wxapp_type']=1;
	}else{
		$item['wxapp_type']=3;
	}
	$default_mobile_web=uni_setting('default_mobile_web');
	if($item['id']==$default_mobile_web){
		$item['is_default']=1;
	}
	$back=cfc('core')->get_back_newss(array('plugin'=>'core','action'=>'welcome','id'=>$id));
	$item=array_merge($back,$item);
	$return['item']=$item;
	$default=array(
		
	);
	$return['wxapp_types']=array(
		'1'=>'关联已有小程序',
		'2'=>'创建同名小程序并关联',
		'3'=>'不关联小程序',
	);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'设置表单页信息',
		'tabs'=>array(
			array('op'=>'list','title'=>'表单页列表'),
		),
		'edit'=>array(
						array('type'=>'string','name'=>'title','title'=>'活动名称','must'=>true),
						cfc('core')->keywords(),
						array('type'=>'image','name'=>'picurl','title'=>'封面图片'),
						array('type'=>'switch','name'=>'is_default','title'=>'设为默认活动'),
						array('type'=>'switch','name'=>'status','title'=>'是否启用'),
						array('type'=>'switch','name'=>'nopc','title'=>'是否关闭PC页面','help'=>'关闭PC页面后，PC端将只访问微官网页面'),
						array('type'=>'color_pool','name'=>'syscolor','title'=>'选择配色方案','help'=>''),
						array('type'=>'colorpicker','name'=>'syscolor','title'=>'微官网主题色','help'=>''),
						array('type'=>'phone_tpl','name'=>'syscolor','demo'=>1,'title'=>'演示效果'),
						array('type'=>'text','name'=>'description','title'=>'页面描述'),
						array('type'=>'string','name'=>'domain','title'=>'绑定域名'),
						array('type'=>'text','name'=>'foot','title'=>'底部自定义'),
						array('type'=>'select','name'=>'menu_id','title'=>'自定义底部菜单','options'=>'menus'),
						//array('type'=>'radio','name'=>'wxapp_type','title'=>'关联小程序设置','options'=>'wxapp_types','关联小程序后，设计微官网的同时，系统会自动将您的设计应用在关联小程序中'),
						//array('type'=>'select','name'=>'wxapp_id','title'=>'选择要关联的小程序','options'=>'wxapps','show'=>array('wxapp_type',1)),
		)
	);
	$return['htmls']=$htmls;
	$return ['menus']=pdo_getall('core_diy_menu',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['keytypes']=get_keyword_types();
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$id=intval($post['id']);
	//更新活动数据
	if(!$post['keywords']){
		exi('请先设置关键词！','error');
	}
	$updata=array_fields(array('title','nopc','status','foot','domain','menu_id','syscolor'),$post);
	if(!$id){
		//添加活动
		model('core/web')->check_num('mobile');
		$updata['uniacid']=$_SESSION['uniacid'];
		$updata['createtime']=TIMESTAMP;
		$updata['type']=3;
		pdo_insert('core_mobile_web',$updata);
		$web_id=pdo_insertid();
	}else{
		$web_id=$id;
		pdo_update('core_mobile_web',$updata,array('id'=>$web_id,'uniacid'=>$_SESSION['uniacid']));	
		if($post['is_default']){
			//设为首页
			uni_setting('default_mobile_web',$id);
		}
	}
	//插入回复规则
	$data=array_fields(array('title','status','description','picurl','keywords'),$post);
	$data['plugin']='core';
	$data['action']='welcome';
	$data['id']=$web_id;
	cfc('core')->save_back_news($data);
	$check=pdo_getcolumn('core_account_wxapp',array('web_id'=>$web_id),'id');
	if(!$check){
			$insert=pdo_get('core_mobile_web',array('id'=>$web_id,'uniacid'=>$_SESSION['uniacid']),array('items','setting'));
			$insert['uniacid']=$_SESSION['uniacid'];
			$insert['type']=1;
			$insert['web_id']=$web_id;
			$insert['createtime']=time();
			$insert['title']=$data['title'];
			$insert['syscolor']=$data['syscolor'];
			pdo_insert('core_account_wxapp',$insert);
			$wxapp_id=pdo_insertid();
			pdo_update('core_mobile_web',array('wxapp_id'=>$wxapp_id),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	exi('保存成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的表单页！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('core_mobile_web',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['is_default'])){
		$default=intval($post['is_default']);
		if($default){
			$default=$id;
		}
		uni_setting('default_mobile_web',$default);
	}
	exi('操作成功！');
}
if($_RQ['op']=='uptpl'){
	$tpl_id=intval($_RQ['id']);
	if(!$tpl_id){
			exi('请先选择要的模板！','error');
	}
	$temid=pdo_getcolumn('core_mobile_template',array('tpl_id'=>$tpl_id),'id');
	if($temid){
		$data=pdo_get('core_account_web_tpl',array('id'=>$tpl_id,'uniacid'=>$_SESSION['uniacid']),array('items','setting','pageaction','plugin_name'));
		$pages=pdo_getall('core_account_web_tpl',array('tpl_id'=>$tpl_id,'uniacid'=>$_SESSION['uniacid']),array('title','items','setting','pageaction','plugin_name'));
		$insert=array(
			'items'=>$data['items'],
			'setting'=>$data['setting'],
			'pageaction'=>$data['pageaction'],
			'plugin_name'=>$data['plugin_name'],
			'pages'=>ijson_encode($pages),
		);
		pdo_update('core_mobile_template',$insert,array('id'=>$temid,'uniacid'=>$_SESSION['uniacid']));
		exi('同步模版成功！');
	}else{
		exi('','','/basic.xiu_form/release/id='.$tpl_id);
	}
}
if($_RQ['op']=='release'){
	//发布到模版市场，发布到模版市场是否需要设置价格和分类？暂时先不发布，直接保存为我的模版
	if($_RQ['postdata']){
		$post=get_postdata();
		$tpl_id=intval($post['tpl_id']);
		if(!$tpl_id){
			exi('请先选择活动！','error');
		}
		$data=pdo_get('core_account_web_tpl',array('id'=>$tpl_id,'uniacid'=>$_SESSION['uniacid']),array('items','setting','pageaction','plugin_name'));
		$pages=pdo_getall('core_account_web_tpl',array('tpl_id'=>$tpl_id,'uniacid'=>$_SESSION['uniacid']),array('title','items','setting','pageaction','plugin_name'));
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'type'=>3,
			'group_id'=>intval($post['group_id']),
			'title'=>$post['title'],
			'image'=>$post['image'],
			'items'=>$data['items'],
			'setting'=>$data['setting'],
			'pageaction'=>$data['pageaction'],
			'plugin_name'=>$data['plugin_name'],
			'pages'=>ijson_encode($pages),
			'tpl_id'=>$tpl_id,
		);
		pdo_insert('core_mobile_template',$insert);
		exi('保存模版成功！','','list');
	}else{
		$id=intval($_RQ['id']);
		if(!$id){
			exi('请先选择要的模板！','error');
		}
		$return['item']=pdo_get('core_mobile_template',array('tpl_id'=>$id),array('id','tpl_id','title','image','group_id'));
		$return['item']=$return['item']?:array('tpl_id'=>$id);
		$return['types']=pdo_getall('core_template_group',array(),array('id','title'));
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'设置模版信息',
			'edit'=>array(
				array('type'=>'string','name'=>'title','title'=>'模版名称'),
				array('type'=>'select','name'=>'group_id','title'=>'模版类型','options'=>'types'),
				array('type'=>'image','name'=>'image','title'=>'模版图片','help'=>'上传一张图片展示模版效果'),
			),
		);
		exi($return);
	}
}
if($_RQ['op']=='release_wxapp'){
	//发布到模版市场，发布到模版市场是否需要设置价格和分类？暂时先不发布，直接保存为我的模版
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['web_id']);
		if(!$id){
			exi('请先选择活动！','error');
		}
		$data=pdo_get('core_mobile_web',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('items','setting'));
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'type'=>3,
			'group_id'=>intval($post['group_id']),
			'title'=>$post['title'],
			'image'=>$post['image'],
			'items'=>$data['items'],
			'setting'=>$data['setting'],
		);
		pdo_insert('core_wxapp_template',$insert);
		exi('保存模版成功！','','list');
	}else{
		$id=intval($_RQ['id']);
		if(!$id){
			exi('请先选择要保存的表单页！','error');
		}
		$return['item']=array('web_id'=>$id);
		$return['types']=pdo_getall('core_template_group',array(),array('id','title'));
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'设置模版信息',
			'edit'=>array(
				array('type'=>'string','name'=>'title','title'=>'模版名称'),
				array('type'=>'select','name'=>'group_id','title'=>'模版类型','options'=>'types'),
				array('type'=>'image','name'=>'image','title'=>'模版图片','help'=>'上传一张图片展示模版效果'),
			),
		);
		exi($return);
	}
}
if($_RQ['op']=='design'){
	//页面设计
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择活动！','error');
		}
		if($post['items']){
			foreach($post['items'] as &$v){
				if($v['extradata']){
					unset($v['extradata']);
				}
			}
		}
		$update=array(
			'items'=>$post['items']?ijson_encode($post['items']):'',
			'setting'=>$post['setting']?ijson_encode($post['setting']):''
		);
		if($update['items'] || $update['setting']){
			//检测恶意代码
			if(strexists($update['items'],'<?') || strexists($update['setting'],'<?')){
				exi('提交内容存在恶意代码，无法提交！','error');
			}
			if($post['type']!='template'){//type==template表示设计的是模版，设计模版时无需生成具体页面			
				//获取开发者自己的组件，并上传
				$local_blocks=pdo_getall('core_mobile_blocks',array('madehtml <>'=>''),array('cloud_id','default','madehtml'),'cloud_id');
				if($local_blocks){
					foreach($local_blocks as &$row){
						$row['madehtml']=cfc('files')->source_read($row['madehtml']);
						$row['default']=$row['default']?json_decode($row['default'],true):array();
					}
				}
				//设计完成后，对接云端生成页面
				$post=array('items'=>base64_encode($update['items']),'setting'=>base64_encode($update['setting']),'local_blocks'=>$local_blocks);
				$html=cloud_request('diy.mobile.made',$post);
				//将获得生成的html代码存入文件
				$item=pdo_get('core_account_web_tpl',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('html'));
				$html=cfc('files')->source_save($html,'page','html','活动页面：'.$id,$item['html']);
				if(!$item['html']){
					$update['html']=$html;
				}
			}
		}
		if($post['type']=='template'){
			pdo_update('core_mobile_template',$update,array('id'=>$id));
			exi('保存模板成功！','','goback');
		}
		$update['updatetime']=TIMESTAMP;
		pdo_update('core_account_web_tpl',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('保存成功！','','goback');
	}else{
		$id=intval($_RQ['id']);
		$art_num=intval($_RQ['art_num']);
		$fenlei_num=intval($_RQ['fenlei_num']);
		$store_num=intval($_RQ['store_num']);
		if(!$id){
			exi('请先选择要设计的表单页！','error');
		}
		
		$return['used_navs']=array();
		if($_RQ['type']=='template'){
			$item=pdo_get('core_mobile_template',array('id'=>$id),array('id','title','items','setting'));
			$item['type']='template';
		}else{
			$item=pdo_get('core_account_web_tpl',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','tpl_id','web_id','title','items','setting'));
			//$_SESSION['mobile_web']['group_id']=$item['group_id'];
			if($item['web_id']){
				$_SESSION['mobile_web']['webid']=$item['webid'];
				if($item['tpl_id']==0){
					$_SESSION['mobile_web']['mod_id']=$id;
				}else{
					$_SESSION['mobile_web']['mod_id']=$item['tpl_id'];
				}
			}else{
				$_SESSION['mobile_web']['webid']=$id;
			}
		}
			
		$item['items']=$item['items']?json_decode($item['items'],true):array(
				array('nid'=>7,'params'=>array('pics'=>array(array('imgurl'=>'../core/resource/images/diy/default/banner-1.jpg')))),
				array('nid'=>52,'params'=>array('posttitle'=>'提交','fileddata'=>array(array('title'=>'姓名','type'=>1),array('title'=>'电话','type'=>2)))),
		);
		if($item['items']){
			foreach($item['items'] as $row){
				if(!$return['used_navs'][$row['nid']]){
					$return['used_navs'][$row['nid']]=pdo_get('core_mobile_blocks',array('cloud_id'=>$row['nid']),array('name','showhtml','edithtml'));
				}
			}
		}
		$item['setting']=$item['setting']?json_decode($item['setting'],true):array('guide'=>array('title1'=>''));
		$navs0=pdo_getall('core_mobile_blocks',array('type'=>0,'basictype'=>0,'support <>'=>2),array(),'','usenum desc',array('10'));
		if($navs0){
			foreach($navs0 as &$nav){
				$nav['default']=json_decode($nav['default'],true);
			}
		}
		$navs1=pdo_getall('core_mobile_blocks',array('type'=>0,'basictype'=>1,'support <>'=>2),array(),'','usenum desc',array('10'));
		if($navs1){
			foreach($navs1 as &$nav){
				$nav['default']=json_decode($nav['default'],true);
			}
		}
		$navs2=pdo_getall('core_mobile_blocks',array('type'=>1,'support <>'=>2),array(),'','usenum desc',array('10'));
		if($navs2){
			$plugintitles=array();
			$plugins=array();
			$i = 0;
			foreach($navs2 as $k=>&$nav){
				$plugintitle=pdo_getcolumn('core_plugins',array('name'=>$nav['plugin_id']),'title');
				$nav['default']=json_decode($nav['default'],true);
				if (in_array($plugintitle,$plugintitles)){
					
				}else{
					$plugintitles[$i] = $plugintitle;
					$plugins[$i]['name'] = $nav['plugin_id'];
					$plugins[$i]['plugintitle']=$plugintitle;
					$blocks = pdo_getall('core_mobile_blocks',array('type'=>1,'plugin_id'=>$nav['plugin_id']),array(),'','usenum desc',array('10'));
					foreach($blocks as &$b){
						$b['default']=json_decode($b['default'],true);
					}
					$plugins[$i]['navs'] = $blocks;
					$i  += 1;
				}
				
			}
		}
		$return['navs0']=$navs0;
		$return['navs1']=$navs1;
		$return['navs2']=$plugins;
		$return['edit']=3;
		$item['setting']['bg']=!empty($item['setting']['bg'])?$item['setting']['bg']:array('bg'=>'');
		$item['setting']['top']['title']=$item['setting']['top']['title']?$item['setting']['top']['title']:$item['title'];
		$return['menus']=pdo_getcolumn('core_diy_menu',array('uniacid'=>$_SESSION['uniacid'],'plugin'=>'core','status'=>1),'data');
		$return['menus']=json_decode($return['menus'],true);
		$item['setting']['footer']['menus']=!empty($item['setting']['footer']['menus']) ? $item['setting']['footer']['menus']:$return['menus']['menus'];
		$item['setting']['footer']['chosecolor']=$item['setting']['footer']['chosecolor']?:$_SESSION['customsyscolor'];
		$return['fenleis']=pdo_getall('core_account_article_fenlei',array('uniacid'=>$_SESSION['uniacid']));
		$return['articles']=pdo_getall('core_account_article',array('uniacid'=>$_SESSION['uniacid']));
		$return['store_group']=pdo_getall('core_account_store_group',array('uniacid'=>$_SESSION['uniacid']));
		$return['article_store']=pdo_getall('core_account_store',array('uniacid'=>$_SESSION['uniacid']));
		$return['account']=$_SESSION['account'];
		$return['item']=$item;
		$return['extradata']=array();
		exi($return);
	}
}
if($_RQ['op']=='link'){
	$list=pdo_getall('core_account_web_tpl',array('tpl_id'=>$_SESSION['mobile_web']['mod_id'],),array('id','title','tpl_id'));
	$list[]=array('title'=>'首页','id'=>$_SESSION['mobile_web']['mod_id']);
	if($list){
		foreach($list as &$item){
			$item['url']='core/welcome/main/pageid='.$item['id'];
		}
	}
	if($_RQ['from']=='action'){
		$query='from=action';
	}else{
		$query='';
	}
	$return['list']=$list;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'link','title'=>'站内页面'),
			array('action'=>'tools.link','query'=>$query,'title'=>'系统其他页面'),
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'pagelist'=>false,'list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal_result','name'=>'url','title'=>'选取'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	exi($return);
}
if($_RQ['op']=='getnav'){
	$params=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
	}
	$navs=pdo_getall('core_mobile_blocks',$params,array(),'','usenum desc',array('10'));
	exi($navs);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_mobile_web',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_mobile_web',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}